<!--
 * @Author: liangzhuo liangzhuo@chzu.com
 * @Date: 2022-10-17 22:13:17
 * @LastEditors: liangzhuo liangzhuo@chzu.com
 * @LastEditTime: 2022-10-24 21:46:32
 * @FilePath: \hongxinUi\src\views\exam\input\index.vue
 * @Description: 试题公用的搜索和一些按钮组件
-->

<template>
  <div>
    <!-- 头部输入框 -->
    <el-form class="top_input" :model="queryParams" size="small" :inline="true">
      <el-form-item label="题干内容">
        <el-input
          v-model="questionText"
          placeholder="请输入题干内容"
          size="small"
          style="width: 240px"
        ></el-input>
      </el-form-item>

      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="handleRefresh"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <!-- 功能按钮 -->
    <el-row class="btn">
      <el-button
        type="success"
        icon="el-icon-plus"
        size="mini"
        @click="handleAdd"
        v-show="isShow"
        >新增</el-button
      >
      <el-button
        type="danger"
        icon="el-icon-delete"
        size="mini"
        @click="handleDelete"
        v-show="isShow"
        >删除</el-button
      >
      <el-button
        type="info"
        icon="el-icon-upload2"
        size="mini"
        @click="handleImport"
        v-show="isShow"
        >导入</el-button
      >
      <el-button
        type="warning"
        icon="el-icon-download"
        size="mini"
        @click="handleExport"
        v-show="isShow"
        >导出</el-button
      >
    </el-row>

    <!-- 试题导入对话框 -->
    <el-dialog :title="upload.title" :visible.sync="upload.open" width="400px">
      <el-upload
        ref="upload"
        :limit="1"
        accept=".xlsx, .xls"
        :headers="upload.headers"
        :action="upload.url + '?updateSupport=' + upload.updateSupport"
        :disabled="upload.isUploading"
        :on-progress="handleFileUploadProgress"
        :http-request="httpRequest"
        :auto-upload="false"
        drag
        >
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">
          将文件拖到此处，或
          <em>点击上传</em>
        </div>
        <div class="el-upload__tip" slot="tip">
          <el-link type="info" style="font-size:12px" @click="importTemplate">下载模板</el-link>
        </div>
        <div class="el-upload__tip" style="color:red" slot="tip">提示：仅允许导入“xls”或“xlsx”格式文件！</div>
      </el-upload>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFileForm">确 定</el-button>
        <el-button @click="upload.open = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getToken } from "@/utils/auth";
import {getExamInput, delExamInput, importExamInput, exportInput} from '@/api/exam/examInput.js'
import {getExamJudge, importExamJudge} from '@/api/exam/examJudge.js'
import {getExamSchoose, importExamSchoose} from '@/api/exam/examSchoose.js'
import {getExamMchoose, importExamMchoose} from '@/api/exam/examMchoose.js'
import { delExamJudge, exportJudge } from "../../api/exam/examJudge";
import { delExamSchoose, exportSchoose } from "../../api/exam/examSchoose";
import { delExamMchoose, exportMchoose } from "../../api/exam/examMchoose";
export default {
  name: 'HongxinUiCommon',
  props:{
    examType: {
      type:String,
      default:"input"
    },
    queryParams: {
      type: Object,
      default: {
        pageNum: 1,
        pageSize: 10,
        questionText: undefined
      }
    },
    multipleSelection: {
      type: Array,
      default: []
    }
  },
  data() {
    return {
      questionText: '', // 问题输入框
      isOpenDialog: false, // 是否打开弹出框
      // 用户导入参数
      upload: {
        // 是否显示弹出层（用户导入）
        open: false,
        // 弹出层标题（用户导入）
        title: "",
        // 是否禁用上传
        isUploading: false,
        // 是否更新已经存在的用户数据
        updateSupport: 0,
        // 设置上传的请求头部
        headers: { Authorization: "Bearer " + getToken() },
        // 上传的地址
        url: process.env.VUE_APP_BASE_API + "/system/user/importData"
      },
      importFile: undefined,
      isShow: true,
    };
  },

  mounted() {
    // 得到全局的变量
    this.isShow = this.$store.getters.isShow
  },

  methods: {
    handleQuery() {
      this.queryParams.questionText = this.questionText
      let examInfo = {
        dataList: [],
        total: 0
      }
      // 判断题
      if (this.examType == 'input') {
        getExamInput(this.queryParams).then(res => {
          examInfo.dataList = res.rows
          examInfo.total = res.total
          // 子组件给父组件传值
          this.$emit('examInfo', examInfo)
        })
      // 判断题
      } else if (this.examType == 'judge') {
        getExamJudge(this.queryParams).then(res => {
          examInfo.dataList = res.rows
          examInfo.total = res.total
          // 子组件给父组件传值
          this.$emit('examInfo', examInfo)
        })
      // 单选
      } else if (this.examType == 'schoose') {
        getExamSchoose(this.queryParams).then(res => {
          examInfo.dataList = res.rows
          examInfo.total = res.total
          // 子组件给父组件传值
          this.$emit('examInfo', examInfo)
        })
      // 多选
      } else if (this.examType == 'mchoose') {
        getExamMchoose(this.queryParams).then(res => {
          examInfo.dataList = res.rows
          examInfo.total = res.total
          // 子组件给父组件传值
          this.$emit('examInfo', examInfo)
        })
      }
    },
    // 重置表单
    handleRefresh() {
      this.questionText = undefined
      this.handleQuery();
    },
    // 新增按钮逻辑
    handleAdd() {
      this.$emit('isOpenDialog', {
        isOpenDialog: true,
        title: '新增试题信息'
      })
    },
    // 删除按钮逻辑
    handleDelete() {
      // 判断题
      if (this.examType == 'input') {
        let ids = []
        this.multipleSelection.forEach(item => {
          ids.push(item.id)
        })
        this.$modal
          .confirm('是否确认删除试题信息吗？')
          .then(function () {
            return delExamInput(ids);
          })
          .then(() => {
            this.handleQuery();
          });
      // 判断题
      } else if (this.examType == 'judge') {
        let ids = []
        this.multipleSelection.forEach(item => {
          ids.push(item.id)
        })
        this.$modal
          .confirm('是否确认删除试题信息吗？')
          .then(function () {
            return delExamJudge(ids);
          })
          .then(() => {
            this.handleQuery();
          });
      // 单选
      } else if (this.examType == 'schoose') {
        let ids = []
        this.multipleSelection.forEach(item => {
          ids.push(item.id)
        })
        this.$modal
          .confirm('是否确认删除试题信息吗？')
          .then(function () {
            return delExamSchoose(ids);
          })
          .then(() => {
            this.handleQuery();
          });
      // 多选
      } else if (this.examType == 'mchoose') {
        let ids = []
        this.multipleSelection.forEach(item => {
          ids.push(item.id)
        })
        this.$modal
          .confirm('是否确认删除试题信息吗？')
          .then(function () {
            return delExamMchoose(ids);
          })
          .then(() => {
            this.handleQuery();
          });
      }
    },
    // 导出按钮逻辑
    handleExport() {
      if (this.examType == 'input') {
        const queryParams = this.queryParams;
        this.$confirm('是否确认导出所有试题数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportInput(queryParams);
        }).then(response => {
          this.download(response.msg);
        }).catch(function() {});
      // 判断题
      } else if (this.examType == 'judge') {
        const queryParams = this.queryParams;
        this.$confirm('是否确认导出所有试题数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportJudge(queryParams);
        }).then(response => {
          this.download(response.msg);
        }).catch(function() {});
      // 单选
      } else if (this.examType == 'schoose') {
        const queryParams = this.queryParams;
        this.$confirm('是否确认导出所有试题数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportSchoose(queryParams);
        }).then(response => {
          this.download(response.msg);
        }).catch(function() {});
      // 多选
      } else if (this.examType == 'mchoose') {
        const queryParams = this.queryParams;
        this.$confirm('是否确认导出所有试题数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportMchoose(queryParams);
        }).then(response => {
          this.download(response.msg);
        }).catch(function() {});
      }
    },
    // 导入按钮逻辑
    handleImport() {
      this.upload.title = "试题导入";
      this.upload.open = true;
    },
    /** 下载模板操作 */
    importTemplate() {
      // 暂不提供
    },
    // 文件上传中处理
    handleFileUploadProgress(event, file, fileList) {
      this.upload.isUploading = true;
    },
    // 文件导入后的处理
    importFileHandle() {
      this.upload.open = false;
      this.upload.isUploading = false;
      this.$refs.upload.clearFiles();
      this.handleQuery();
    },
    // 文件导入处理
    httpRequest(param) {
      const formData = new FormData()
      formData.append('file', param.file)
      if (this.examType == 'input') {
        importExamInput(formData).then(res => {
          this.$message({
            message: '试题导入成功',
            type: 'success'
          });
          this.importFileHandle()
        })
      // 判断题
      } else if (this.examType == 'judge') {
        const formData = new FormData()
        formData.append('file', param.file)
        importExamJudge(formData).then(res => {
          this.$message({
            message: '试题导入成功',
            type: 'success'
          });
          this.importFileHandle()
        })
      // 单选
      } else if (this.examType == 'schoose') {
        const formData = new FormData()
        formData.append('file', param.file)
        importExamSchoose(formData).then(res => {
          this.$message({
            message: '试题导入成功',
            type: 'success'
          });
          this.importFileHandle()
        })
      // 多选
      } else if (this.examType == 'mchoose') {
        const formData = new FormData()
        formData.append('file', param.file)
        importExamMchoose(formData).then(res => {
          this.$message({
            message: '试题导入成功',
            type: 'success'
          });
          this.importFileHandle()
        })
      }
    },
    // 提交上传文件
    submitFileForm() {
      this.$refs.upload.submit();
    }
  },
};
</script>

<style lang="scss" scoped>
.top_input {
  margin-left: 15px;
  margin-top: 15px;
}
.btn {
  margin-left: 10px;
  margin-bottom: 10px;
}
</style>